package com.renwei.yunlong.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.LevelListAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.view.BasePopwindow;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 证书名称--页面
 * <p>
 * Created by Administrator on 2017/8/23.
 */

public class UserCertificateNameActivity extends BaseActivity implements View.OnClickListener, LevelListAdapter.LevelChangeListener {


    String action;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_edit)
    TextView tvEdit;
    @Bind(R.id.iv_cont)
    ImageView ivCont;
    @Bind(R.id.iv_edit)
    ImageView ivEdit;
    @Bind(R.id.iv_contacts)
    ImageView ivContacts;
    @Bind(R.id.iv_more)
    ImageView ivMore;
    @Bind(R.id.iv_scroll)
    ImageView ivScroll;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.tv_level)
    TextView tvLevel;
    @Bind(R.id.rl_phone)
    RelativeLayout rlPhone;
    private BasePopwindow popupWindow;
    private String level;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_user_certificate_name);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        initData();
    }

    private void initData() {
        tvEdit.setVisibility(View.VISIBLE);
        tvEdit.setText("保存");
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvEdit.setOnClickListener(this);
        popupWindow = new BasePopwindow(this, "");
        View view = LayoutInflater.from(this).inflate(R.layout.popu_certificate_level, null, false);
        RecyclerView xLevelList = view.findViewById(R.id.leve_list);
        xLevelList.setLayoutManager(new LinearLayoutManager(this));
        LevelListAdapter adapter = new LevelListAdapter(this);
        adapter.setLevelChangeListener(this);
        xLevelList.setAdapter(adapter);
        popupWindow.initUI(view);
        etPhone.setInputType(InputType.TYPE_CLASS_TEXT);
        Intent intent = getIntent();
        action = getIntent().getAction();
        if ("1".equals(action)) {
            tvTitle.setText("证件名称");
            String name = intent.getStringExtra("name") == null ? "" : intent.getStringExtra("name");
            rlPhone.setBackgroundColor(getResources().getColor(R.color.white));
            etPhone.setVisibility(View.VISIBLE);
            tvLevel.setVisibility(View.GONE);
            if (name != null && name.length()>0) {
                etPhone.setSelection(name.length() - 1);
            }
            etPhone.setText(name);
        } else {
            rlPhone.setBackground(null);
            tvTitle.setText("证件等级");
            etPhone.setVisibility(View.GONE);
            tvLevel.setVisibility(View.VISIBLE);
            tvLevel.setOnClickListener(this);
            String level = intent.getStringExtra("level") == null ? "" : intent.getStringExtra("level");
            if (level != null && level.length() > 0) {
                tvLevel.setText(level);
            } else {
                tvLevel.setText("无等级");
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_edit:
                if ("1".equals(action)) {
                    setResult(1, new Intent().putExtra("name", etPhone.getText().toString()));
                } else {
                    setResult(2, new Intent().putExtra("level", tvLevel.getText().toString()));
                }
                finish();
                break;
            case R.id.tv_level:
                if (!"1".equals(action)) {
                    popupWindow.showAsDropDownWhite(findViewById(R.id.pop_view), 0, 10);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void click(int position, String name) {
        tvLevel.setText(String.valueOf(name));
        popupWindow.dismiss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
